package com.bajiao.service.useRule.impl;

import com.bajiao.common.util.StringUtils;
import com.bajiao.dto.useRule.UseRulePageVo;
import com.bajiao.entity.useRule.UseRule;
import com.bajiao.mapper.useRule.UseRuleMapper;
import com.bajiao.service.useRule.UseRuleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 使用规则
 * @Author: wangw
 * @Date:   2025.03.01
 * @Version: V1.0
 */
@Service
public class UseRuleServiceImpl extends ServiceImpl<UseRuleMapper, UseRule> implements UseRuleService {

    @Override
    public IPage<UseRule> queryPage(UseRulePageVo vo) {
        Page<UseRule> page = new Page();
        page.setCurrent(vo.getPageNo());
        page.setSize(vo.getPageSize());

        QueryWrapper<UseRule> wrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(vo.getTitle())){
            wrapper.lambda().like(UseRule::getTitle, vo.getTitle());
        }

        IPage<UseRule> iPage = this.page(page,wrapper);
        return iPage;
    }

    @Override
    public boolean edit(UseRulePageVo vo) {
        UseRule useRule = new UseRule();
        BeanUtils.copyProperties(vo, useRule);
        return this.updateById(useRule);
    }

    @Override
    public boolean addUseRule(UseRulePageVo vo) {
        UseRule useRule = new UseRule();
        BeanUtils.copyProperties(vo, useRule);
        return this.save(useRule);
    }

    @Override
    public boolean del(String id) {
        return this.removeById(Integer.valueOf(id));
    }
}
